#include <TinyXml.h>
#include <kernel/tinyxml2/tinyxml2.h>
#include <log/log_err.h>
using namespace tinyxml2;

TinyXml::TinyXml(/* args */)
{

}

TinyXml::~TinyXml()
{

}

/* 查找 root 中 element节点中的 attribute属性为value的节点 */
XMLElement* TinyXml::findAttribute(XMLElement* root, const char* element, 
            const char* attribute, const char* value)
{
    bool failed = true;
    XMLError errorId = XML_SUCCESS;
    const char* buffer = "failed";
    XMLElement* ret = root->FirstChildElement(element);
    SIGNWAY_ERROR_GOTO(ret == nullptr, RETURN, "not find %s", element);
    do
    {
        errorId = ret->QueryStringAttribute( attribute, &buffer);
        SIGNWAY_ERROR_CONTINUE(errorId > 0, "not find %s attribute", attribute);

        // SIGNWAY_LOGI("%s", buffer);
        if (strcmp(buffer, value) == 0) {
            failed = false;
            break;
        }

    } while (ret = ret->NextSiblingElement(element));

RETURN:
    if (failed) {
        return nullptr;
    }
    return ret;
}